package run.autoium.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import run.autoium.common.DataCode.response.R;
import run.autoium.entity.po.ProjectParam;
import run.autoium.entity.vo.ProjectParamVo;
import run.autoium.service.impl.ProjectParamServiceImpl;


/**
 * <p>
 * 参数化 前端控制器
 * </p>
 *
 * @author Ks1r
 * @since 2020-11-28
 */
@RestController
@RequestMapping("/param")
@CrossOrigin
public class ProjectParamController {

    @Autowired
    private ProjectParamServiceImpl projectParamService;

    /**
     * 分页查询数据库中的参数
     *
     * @return
     */
    @PostMapping("/pageParam/{current}/{limit}")
    public R pageParamList(@PathVariable Long current,
                           @PathVariable Long limit,
                           @RequestBody ProjectParamVo queryVo) {
        // 获取page
        Page<ProjectParam> page = new Page<>(current, limit);
        ProjectParamVo[] records = projectParamService.recordQuery(page, queryVo);

        return R.ok().data("total", records.length).data("records", records);
    }

    /**
     * 根据id获取一个参数
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R getProjectParamInfoById(@PathVariable("id") String id) {
        ProjectParam item = projectParamService.getById(id);
        System.out.println(item);
        return R.ok().data("item", item);
    }

    /**
     * 新增一个项目的参数
     *
     * @param projectParam
     * @return
     */
    @PostMapping("/addParam")
    public R addParam(@RequestBody ProjectParam projectParam) {
        boolean flag = projectParamService.save(projectParam);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 根据id删除参数
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public R removeParamById(@PathVariable("id") String id) {
        boolean b = projectParamService.removeById(id);
        if (b) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    /**
     * 修改参数
     *
     * @param projectParam
     * @return
     */
    @PostMapping("/update")
    public R updateParam(@RequestBody ProjectParam projectParam) {
        boolean b = projectParamService.updateById(projectParam);
        if (b) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }
}

